//<SCRIPT LANGUAGE=javascript>
//******* Fields in Row *********
function __row(row){
	var table = __tblName();
	try{__row.doc = row.document;
	}catch(e){__row.doc == null}
	var oTbl = (__row.doc||document).all[table];
	if( defined(row) ){
		if( typeof(row) == "number" )row = oTbl.rows[row];
		oTbl.selRow = __getRow(row);
	}
	return oTbl.selRow;
}
function __field(value,container){
var table = __tblName();
var row = __row();
	if( !(row && row.parentElement) )return null;//alert("Row is not selected in \""+table+"\".");
	var name = __fldName();
	var fld = findElementByAttr(row,"dataFld",name);
	if( !fld )return alert("No element with dataFld=\""+name+"\" found in \""+table+"\".");
	if( defined(value) )setElementValue(fld,value);
	return container? fld : (defined(value)?value:getElementValue(fld));
}
function __tblName(callerName){
var tbl =	(callerName||__callerName());
	if(tbl.indexOf("_ms__id")==0)
		return tbl.match(/ms__id[^_]+/)[0];
	return "tbl"+tbl.match(/^_([^_]+)/)[1];
}
function __fldName(callerName){return (callerName||__callerName()).match(/.+_(.+)$/)[1];}
function __callerName(){
var funcName = getFuncName_DB(__callerName.caller.caller.caller);
	return funcName != "__field" ? funcName : getFuncName_DB(__callerName.caller.caller.caller.caller);
}
function __findRow(idFunc,id,funcSet,value){
	var tbl = __tblName(getFuncName_DB(idFunc));
	var rowFunc = tbl.replace(/^tbl/,"_");
	try{rowFunc = eval(rowFunc);
	}catch(e){return alert("Function \""+rowFunc+"\" is not found in Document.")}
	try{tbl = eval(tbl);
	}catch(e){return alert("Table \""+tbl+"\" is not found in Document.")}
	var rowCur = rowFunc() || null;
	for( var i=0; i < tbl.rows.length; i++ ){
		rowFunc(i);
		if( idFunc() == id ){
			var rowRet = rowFunc();
			if( funcSet )funcSet(value);
			rowFunc(rowCur)
			return rowRet;
		}
	}
	return null;
}
/////////////////////////////////////////////////
function __fieldByName(el,fieldName,fieldValue){
	__makeRowFunc(el)(el);
	return __makeFieldFunc(el,fieldName)(fieldValue);
}
function __makeFieldFunc(table,fieldName){
	table = __getTable(table);
var fldName =	"_"+(table.uniqueID)+"_"+fieldName;
var sFunc = __fieldFuncTempl.toString().replace(/__[^(]+/,fldName);
	eval("var func = "+sFunc);
	return func;
}
function __makeRowFunc(table){
	table = __getTable(table);
var tblName =	"_"+(table.uniqueID);
var sFunc = __rowFuncTempl.toString().replace(/__[^(]+/,tblName);
	eval("var func = "+sFunc);
	return func;
}
function __getRow(row){
var el = typeof(row) == 'string' ? eval(row) : row;
	if( !defined(row) )throw new Error(0,"__getRow(row)\n\"row\" must be string or object");
	if( el.tagName.toLowerCase() != 'tr' )
		el = getParentByTag(el,"tr");
	if( !el )throw new Error(0,"Can't find a parent row for \""+(row.id||row)+"\!");
	return el;
}
function __getTable(table){
var el = typeof(table) == 'string' ? eval(table) : table;
	if( !defined(table) )throw new Error(0,"__getTable(table)\n\"table\" must be string or object");
	if( el.tagName.toLowerCase() != 'table' )
		el = getParentByTag(el,"table");
	if( !el )throw new Error(0,"Can't find a parent table for \""+(table.id||table)+"\!");
	return el;
}
function __rowFuncTempl(row){return __row(row)}
function __fieldFuncTempl(value)	 {return __field(value) || "";}
//********************************
function getParentByTag(el,tagName){return getParentElementByTagName(el,tagName)}
function getParentElementByTagName(el,parentName){
	parentName = parentName.toLowerCase();
	for( el = el.parentElement; el; el = el.parentElement )
		if( el.tagName.toLowerCase() == parentName )return el;
		return null;
}
function getFuncName_DB(func){
var a = arguments.length? func : getFuncName_DB.caller;
	if( !a )return "";
	a = a.toString().match(/function ([^\(\s]+)/)
	if( a && a[1] )return a[1];
	return "";
}
function getElementsByAttr(parent,attrName,value){
var all = parent.all;
	for( var i=0,els=new Array(); i < all.length; i++ )
		if( defined(all[i][attrName]) && (!defined(value) || all[i][attrName] == value) )
			els[els.length] = all[i];
	return els;
}
function findElementByAttr(node,attrName,attrValue,all){
var aFlds = new Array();
var re = new RegExp("^"+attrValue+"$","i");
var n;
	for(var i=0; i < node.all.length; i++ ){
		n = node.all[i];
		if( n.tagName != "OPTION" && defined(n[attrName]) && 
				(!defined(attrValue) || n[attrName].match(re)) ){
			aFlds[aFlds.length] = n;
			if( !all )break;
		}
	}
	return all ? aFlds : (aFlds[0] || null);
	function defined(v){return typeof(v) != 'undefined'}
}
